<?php
	
	/* Cette classe permet de générer les formulaire de mise à jour de la BDD
	 * de l'admin, la méthode getForm prend pour l'instant en paramètre la requête
	 * SQL et la table. L'algorithme se charge ensuite d'afficher dans des champs
	 * cachés l'ID de l'enregistrement et la table.
	 */
	
	include_once "inc/defines.php";
	
	class formhandler {
		private $formCode;
		
		public function getForm($sqlQuery, $table, $champsAjouter) {
			$db = new PDO(SERVER.":host=".HOST.";dbname=".BASE, USER, PASS);
			$st = $db->prepare($sqlQuery);
			$st->execute();
			$etudPage = false;
			$ddePage = false;
			$idEtud = "";
			$idDDE = "";
			$this->formCode .= "<div class=\"div-table\"><div class=\"div-table-row\">";
			for($i = 0; $i < count($champsAjouter); $i+=4)
				if($champsAjouter[$i + 3] != "")
					$this->formCode .= "<div class=\"div-table-col columnLabel\">".$champsAjouter[$i + 3]." :</div>";
			$this->formCode .= "</div>";
			while($ligne = $st->fetch(PDO::FETCH_ASSOC)) {
				$this->formCode .= "<div class=\"div-table-row\">";
				$this->formCode .= "<form action=\"../../php/doaction.php\" method=\"post\">";
				$this->formCode .= "<input type=\"hidden\" name=\"table\" value=\"".$table."\">";
				foreach($ligne as $key=>$item) {
					if($key == "id_etud") {
						$idEtud = $item;
						$etudPage = true; //si on est dans le cas de la gestion des etudiants on affiche le bouton pour acceder a la fiche de l'etudiant
					}
					else if($key == "id_dde") {
						$idDDE = $item;
						$ddePage = true; //pareil pour les DDE
					}
					if(strpos($key, "id") === FALSE) {
						$this->formCode .= "<div class=\"div-table-col\">";
						if($key == "password")
							$this->formCode .= "<input class=\"formElement\" name=\"".$key."\" type=\"password\" value=\"".$item."\">";
						else
							$this->formCode .= "<input class=\"formElement\" name=\"".$key."\" type=\"text\" value=\"".$item."\">";
						$this->formCode .= "</div>";
					}
					else {
						$this->formCode .= "<input name=\"".$key."\" type=\"hidden\" value=\"".$item."\">";
					}
				}
				$this->formCode .= "<div class=\"div-table-col div-table-buttons\">";
				$this->formCode .= "<input class=\"formElement\" name=\"button\" type=\"submit\" value=\"Appliquer\">";
				$this->formCode .= "</div>";
				$this->formCode .= "<div class=\"div-table-col div-table-buttons\">";
				$this->formCode .= "<input class=\"formElement\" name=\"button\" type=\"submit\" value=\"Supprimer\" onclick=\"return confirm('Voulez-vous vraiment supprimer cet &eacute;l&eacute;ment ?')\"><br>";
				$this->formCode .= "</div>";
				if($etudPage) {
					$this->formCode .= "<div class=\"div-table-col div-table-buttons\">";
					$this->formCode .= "<input class=\"formElement\" type=\"button\" value=\"Fiche &eacute;tudiant\" onclick=\"window.open('../fiche_etu.php?id=".$idEtud."', 'popUpWindow','height=453,width=418,left=0,top=0,resizable=yes,scrollbars=yes,toolbar=yes,menubar=no,location=no,directories=no,status=yes');\">";
					$this->formCode .= "</div>";
				}
				else if($ddePage) {
					$this->formCode .= "<div class=\"div-table-col div-table-buttons\">";
					$this->formCode .= "<input class=\"formElement\" type=\"button\" value=\"Fiche DDE\" onclick=\"window.open('../admin/fiche_dde.php?id=".$idDDE."', 'popUpWindow','height=453,width=418,left=0,top=0,resizable=yes,scrollbars=yes,toolbar=yes,menubar=no,location=no,directories=no,status=yes');\">";
					$this->formCode .= "</div>";
				}
				$this->formCode .= "</form>";
				$this->formCode .= "</div>";
			}
			$this->formCode .= "</div>";
			if($st->rowCount() != 0)
				$this->formCode .= "<hr>";
			$this->formCode .= "<div class=\"div-table\"><div class=\"div-table-row\">";
			$this->formCode .= "<form action=\"../../php/doaction.php\" method=\"post\">";
			$this->formCode .= "<input type=\"hidden\" name=\"table\" value=\"".$table."\">";
			for($i = 0; $i < count($champsAjouter); $i+=4) {
				if($champsAjouter[$i + 2]) {
					$this->formCode .= "<div class=\"div-table-col\">";
					$this->formCode .= "<input class=\"formElement\" name=\"".$champsAjouter[$i]."\" type=\"text\" value=\"".$champsAjouter[$i + 1]."\" placeholder=\"".$champsAjouter[$i + 3]."\">";
					$this->formCode .= "</div>";
				}
				else
					$this->formCode .= "<input name=\"".$champsAjouter[$i]."\" type=\"hidden\" value=\"".$champsAjouter[$i + 1]."\">";
			}
			$this->formCode .= "<div class=\"div-table-col\">";
			$this->formCode .= "<input class=\"formElement\" name=\"button\" type=\"submit\" value=\"Ajouter\">";
			$this->formCode .= "</div>";
			$this->formCode .= "</form>";
			$this->formCode .= "</div>";
			$this->formCode .= "</div>";
			if(isset($_SESSION["sqlError"])) {
				echo "<p class=\"errorMessage\">".$_SESSION["sqlError"]."</p>";
				unset($_SESSION["sqlError"]);
			}
			$db = null;
			echo $this->formCode;
		}
		
		public function getSearchForm() {
			$this->formCode = "<form action=\"../../php/dosearch.php\" method=\"post\">";
			$this->formCode .= "<table cellpadding=5><tr>";
			$this->formCode .= "<td><label for=\"apogee\">Code Apogee :</label></td>";
			$this->formCode .= "<td><input class=\"formElement\" id=\"apogee\" name=\"apogee\" type=\"text\" placeholder=\"N/A\"></td></tr>";
			$this->formCode .= "<tr><td><label for=\"nom\">Nom :</label></td>";
			$this->formCode .= "<td><input class=\"formElement\"  id=\"nom\" name=\"nom\" type=\"text\" placeholder=\"N/A\"></td></tr>";
			$this->formCode .= "<tr><td><label for=\"prenom\">Pr&eacute;nom :</label></td>";
			$this->formCode .= "<td><input class=\"formElement\" id=\"prenom\" name=\"prenom\" type=\"text\" placeholder=\"N/A\"></td></tr>";
			$this->formCode .= "<tr><td><label for=\"login\">Login :</label></td>";
			$this->formCode .= "<td><input class=\"formElement\"  id=\"login\" name=\"login\" type=\"text\" placeholder=\"N/A\"></td></tr>";
			$this->formCode .= "<tr><td><input class=\"formElement searchButton\"  type=\"submit\" value=\"Valider\"></td><td></td>";
			$this->formCode .= "</tr></table>";
			$this->formCode .= "</form>";
			echo $this->formCode;
		}
		
		public function getNoteForm($idEtud, $idExamen) {
			$db = new PDO(SERVER.":host=".HOST.";dbname=".BASE, USER, PASS);
			$sqlQuery = "SELECT id_note, id_etud, id_examen, note, simulee FROM ".NOTE_TABLE." WHERE id_etud=".$idEtud." AND id_examen=".$idExamen.";";
			$st = $db->prepare($sqlQuery);
			$st->execute();
			$this->formCode .= "<div class=\"div-table\"><div class=\"div-table-row\">";
			$this->formCode .= "<div class=\"div-table-col columnLabel\">Note :</div>";
			$this->formCode .= "<div class=\"div-table-col columnLabel\">Simul&eacute;e :</div>";
			$this->formCode .= "</div>";
			$ligne = $st->fetch(PDO::FETCH_ASSOC); //il n'y a qu'une seule note pour un examen
			$this->formCode .= "<div class=\"div-table-row\">";
			$this->formCode .= "<form action=\"../../php/donoteaction.php\" method=\"post\">";
			if(isset($ligne["id_note"])) { //une note est deja saisie pour cet examen
				$this->formCode .= "<div class=\"div-table-col\">";
				$this->formCode .= "<input class=\"formElement\" type=\"hidden\" name=\"id_note\" value=\"".$ligne["id_note"]."\">";
				$this->formCode .= "<input class=\"formElement\" type=\"hidden\" name=\"id_etud\" value=\"".$ligne["id_etud"]."\">";
				$this->formCode .= "<input class=\"formElement\" type=\"hidden\" name=\"id_examen\" value=\"".$ligne["id_examen"]."\">";
				$this->formCode .= "<input class=\"formElement\" type=\"text\" name=\"note\" value=\"".$ligne["note"]."\">";
				$this->formCode .= "</div>";
				$this->formCode .= "<div class=\"div-table-col colCheckBox\">";
				$this->formCode .= "<input type=\"checkbox\" id=\"simulee\" name=\"simulee\"";
				if($ligne["simulee"] == true)
					$this->formCode .= " checked";
				$this->formCode .= ">";
				$this->formCode .= "</div>";
				$this->formCode .= "<div class=\"div-table-col div-table-buttons\">";
				$this->formCode .= "<input class=\"formElement\" type=\"submit\" name=\"button\" value=\"Appliquer\">";
				$this->formCode .= "</div>";
				$this->formCode .= "<div class=\"div-table-col div-table-buttons\">";
				$this->formCode .= "<input class=\"formElement\" type=\"submit\" name=\"button\" value=\"Supprimer\">";
				$this->formCode .= "</div>";
			}
			else { //aucune note n'a encore ete saisie pour cet examen
				$this->formCode .= "<div class=\"div-table-col\">";
				$this->formCode .= "<input class=\"formElement\" type=\"hidden\" name=\"id_note\" value=\"NULL\">";
				$this->formCode .= "<input class=\"formElement\" type=\"hidden\" name=\"id_etud\" value=\"".$idEtud."\">";
				$this->formCode .= "<input class=\"formElement\" type=\"hidden\" name=\"id_examen\" value=\"".$idExamen."\">";
				$this->formCode .= "<input class=\"formElement\" type=\"text\" name=\"note\" value=\"\" placeholder=\"Note\">";
				$this->formCode .= "</div>";
				$this->formCode .= "<div class=\"div-table-col colCheckBox\">";
				$this->formCode .= "<input type=\"checkbox\" id=\"simulee\" name=\"simulee\">";
				$this->formCode .= "</div>";
				$this->formCode .= "<div class=\"div-table-col\">";
				$this->formCode .= "<input class=\"formElement\" type=\"submit\" name=\"button\" value=\"Ajouter\">";
				$this->formCode .= "</div>";
			}
			$this->formCode .= "</form>";
			$this->formCode .= "</div>";
			$this->formCode .= "</div>";
			if(isset($_SESSION["sqlError"])) {
				echo "<p class=\"errorMessage\">".$_SESSION["sqlError"]."</p>";
				unset($_SESSION["sqlError"]);
			}
			$db = null;
			echo $this->formCode;
		}
	}
	
?>
